﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormSelectProductType : FormTemplet
    {
        private static ProductTypeData s_productTypeInfo = null;

        public FormSelectProductType()
        {
            this.InitializeComponent();
            FormSelectProductType.s_productTypeInfo = null;
        }

        private void FormSelectProductType_Load(object sender, EventArgs e)
        {
            this.cbCondition.SelectedIndex = 0;
            string strQuery = "SELECT PKID AS 序号,[Name] AS 类型名称,Thick AS 厚度规格,Unit AS 单位,CAST( DefaultPrice AS decimal(9,2) ) as 默认价格,Remark AS 备注 FROM ProductType order by [Sequence]";
            ProductTypes productTypes = new ProductTypes();
            DataTable dataTable = null;
            try
            {
                dataTable = productTypes.GetProductTypes(strQuery);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            productTypes.Dispose();
            if (dataTable != null)
            {
                this.dgProductType.DataSource = null;
                this.dgProductType.DataSource = dataTable;
                this.dgProductType.AutoResizeColumns();
                this.dgProductType.Focus();
                dataTable.Dispose();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string text = "SELECT PKID AS 序号,[Name] AS 类型名称,Thick AS 厚度规格,Unit AS 单位,CAST( DefaultPrice AS decimal(9,2) ) as 默认价格,Remark AS 备注 FROM ProductType";
            if (this.cbCondition.SelectedIndex == 0)
            {
                text = text + " WHERE dbo.GetPY([Name]) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            if (this.cbCondition.SelectedIndex == 1)
            {
                text = text + " WHERE dbo.GetPY(Thick) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            if (this.cbCondition.SelectedIndex == 2)
            {
                text = text + " WHERE dbo.GetPY(Remark) LIKE '%" + this.txtCondition.Text.Trim().Replace("'", "''").Replace(" ", "%") + "%'";
            }
            text += " order by [Sequence]";
            ProductTypes productTypes = new ProductTypes();
            DataTable dataTable = null;
            try
            {
                dataTable = productTypes.GetProductTypes(text);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n出现未知错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            productTypes.Dispose();
            if (dataTable != null)
            {
                this.dgProductType.DataSource = null;
                this.dgProductType.DataSource = dataTable;
                this.dgProductType.AutoResizeColumns();
                dataTable.Dispose();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dgProductType.Rows.Count > 0)
            {
                int rowIndex = this.dgProductType.CurrentCell.RowIndex;
                FormSelectProductType.s_productTypeInfo = new ProductTypeData(Convert.ToInt32(this.dgProductType.Rows[rowIndex].Cells["序号"].Value), this.dgProductType.Rows[rowIndex].Cells["类型名称"].Value.ToString(), this.dgProductType.Rows[rowIndex].Cells["厚度规格"].Value.ToString(), this.dgProductType.Rows[rowIndex].Cells["单位"].Value.ToString(), Convert.ToDecimal(this.dgProductType.Rows[rowIndex].Cells["默认价格"].Value), this.dgProductType.Rows[rowIndex].Cells["备注"].Value.ToString());
            }
            else
            {
                MessageBox.Show("表格中没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            base.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void cbCondition_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnQuery;
        }

        private void txtCondition_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnQuery;
        }

        private void dgProductType_Enter(object sender, EventArgs e)
        {
            base.AcceptButton = this.btnOk;
        }

        public static ProductTypeData ShowBox()
        {
            FormSelectProductType formSelectProductType = new FormSelectProductType();
            formSelectProductType.TopMost = true;
            formSelectProductType.ShowDialog();
            return FormSelectProductType.s_productTypeInfo;
        }

        private void dgProductType_DoubleClick(object sender, EventArgs e)
        {
            this.btnOk_Click(null, null);
        }

        private void dgProductType_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.btnOk_Click(null, null);
            }
            if (e.KeyCode == Keys.Up && (this.dgProductType.CurrentRow == null || this.dgProductType.CurrentRow.Index < 1))
            {
                this.txtCondition.Focus();
            }
        }

        private void txtCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                this.dgProductType.Focus();
            }
        }
    }
}
